from PyQt5 import QtCore, QtGui, QtWidgets
from New_Gui import Ui_MainWindow
import os
import functions


class Functionality(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(Functionality, self).__init__()
        self.setupUi(self)

        self.table = None
        self.arr = [self.checkBox_Bx, self.checkBox_By_GSE, self.checkBox_Bz_GSE, self.checkBox_By_GSM,
                    self.checkBox_Bz_GSM, self.checkBox_SYM_D, self.checkBox_SYM_H, self.checkBox_ASY_D,
                    self.checkBox_ASY_H, self.checkBox_Vx, self.checkBox_Vy, self.checkBox_Vz]  # Bx By Bz By Bz Sym/d Sym/h Asy/d Asy/h
        self.win2 = None
        self.lineEdit_7.setDisabled(True)
        self.lineEdit_6.setDisabled(True)
        self.lineEdit_3.setDisabled(True)
        self.comboBox_2.setDisabled(True)

        self.files_list()
        self.Handel_button()

    def Handel_button(self):
        self.pushButton_Download.clicked.connect(self.Get_link)
        self.pushButton.clicked.connect(self.simple_Plot)
        self.radioButton.clicked.connect(self.default)
        self.pushButton_SaveConfig.clicked.connect(self.Save_config)

        self.pushButton_4.clicked.connect(self.Marginau_python)
        self.pushButton_2.clicked.connect(self.WignerVille_python)
        self.pushButton_3.clicked.connect(self.Page_python)
        self.pushButton_6.clicked.connect(self.Spectrogram_python)

        self.pushButton_7.clicked.connect(self.Born_Jordan_octave)
        self.pushButton_14.clicked.connect(self.Page_octave)
        self.pushButton_12.clicked.connect(self.Pseudo_Wigner_Ville_octave)
        self.pushButton_8.clicked.connect(self.Choi_Williams_octave)
        self.pushButton_9.clicked.connect(self.Margenau_Hill_octave)
        self.pushButton_17.clicked.connect(self.Spectrogram_octave)
        self.pushButton_15.clicked.connect(self.Pseudo_Page_octave)
        self.pushButton_16.clicked.connect(self.Rihaczek_octave)
        self.pushButton_13.clicked.connect(self.WignerVille_octave)
        self.pushButton_11.clicked.connect(self.Pseudo_Margenau_Hill_octave)
        self.pushButton_10.clicked.connect(self.Margenau_Hill_Spectrogram_octave)
        self.pushButton_18.clicked.connect(self.Zhao_Atlas_Marks_octave)

        self.pushButton_20.clicked.connect(self.Histogram_function)
        self.pushButton_21.clicked.connect(self.Entropy_function)
        self.pushButton_22.clicked.connect(self.Escort_function)
        self.pushButton_23.clicked.connect(self.Inf_Dicrimination_function)

    def Get_link(self):

        if self.lineEdit_5.text() == '':
            name = self.lineEdit_5.placeholderText()
        else:
            name = self.lineEdit_5.text()
        url = self.lineEdit.text()

        functions.Download(url, name)

        self.pushButton_Download.setStyleSheet("background-color: rgb(115, 210, 22);")
        self.pushButton_Download.setText(QtCore.QCoreApplication.translate("MainWindow", "Completed"))
        self.files_list()

    def files_list(self):

        for j, i in enumerate(os.listdir(os.getcwd())):
            self.comboBox.addItem("")
            self.comboBox.setItemText(j, QtCore.QCoreApplication.translate("MainWindow", i))

    def simple_Plot(self):

        if self.table is None:
            self.table = functions.ReadTable(self.comboBox.currentText())

        for i in self.arr:
            if i.isChecked():
                functions.plot(i, self.table)

    def default(self):

        if self.radioButton.isChecked():
            self.comboBox_2.setDisabled(True)
            self.lineEdit_7.setDisabled(True)
            self.lineEdit_6.setDisabled(True)
            self.lineEdit_3.setDisabled(True)
        else:
            self.comboBox_2.setDisabled(False)
            self.lineEdit_7.setDisabled(False)
            self.lineEdit_6.setDisabled(False)
            self.lineEdit_3.setDisabled(False)

    def Refresh(self):
        self.pushButton_SaveConfig.setStyleSheet("background-color: rgb(243, 243, 243);")
        self.pushButton_SaveConfig.setText(QtCore.QCoreApplication.translate("MainWindow", "Save configuration"))

    def Save_config(self):

        if self.table is None:
            self.table = functions.ReadTable(self.comboBox.currentText())

        self.step = int(self.lineEdit_2.text())

        self.num, self.win, self.dsamp = self._window_downsampling_numbers()

        if self.win2 != self.win:
            self.h = functions.Win_functions(self.win)
        self.win2 = self.win
        print("Complete windows")

        self.pushButton_SaveConfig.setStyleSheet("background-color: rgb(115, 210, 22);")
        self.pushButton_SaveConfig.setText(QtCore.QCoreApplication.translate("MainWindow", "Saved"))

    def _window_downsampling_numbers(self):
        if self.lineEdit_7.text() == '':
            n = int(self.lineEdit_7.placeholderText())
        else:
            n = int(self.lineEdit_7.text())

        if self.lineEdit_3.text() == '':
            w = (int(self.lineEdit_3.placeholderText()), self.comboBox_2.currentText())
        else:
            w = (int(self.lineEdit_3.text()), self.comboBox_2.currentText())

        if self.lineEdit_6.text() == '':
            ds = int(self.lineEdit_6.placeholderText())
        else:
            ds = int(self.lineEdit_6.text())

        return n, w, ds

    """Python Plot"""

    def WignerVille_python(self):

        for i in self.arr:
            if i.isChecked():

                functions.WV_python(i, self.table, self.win,
                            self.dsamp, self.num, self.step)

    def Page_python(self):

        for i in self.arr:
            if i.isChecked():
                functions.P_python(i, self.table, self.win, self.dsamp, self.num, self.step)

    def Marginau_python(self):

        for i in self.arr:
            if i.isChecked():
                functions.MH_python(i, self.table, self.win, self.dsamp, self.num, self.step)

    def Spectrogram_python(self):

        for i in self.arr:
            if i.isChecked():
                functions.Spectr_python(i, self.table, self.win, self.dsamp, self.num, self.step)

    """Octave Plot"""

    def WignerVille_octave(self):

        for i in self.arr:
            if i.isChecked():
                self.Refresh()
                info = [i.text(), self.win, self.dsamp]
                functions.WV_octave(i, self.table, self.h, self.dsamp, self.num, self.step, info)

    def Born_Jordan_octave(self):
        for i in self.arr:
            if i.isChecked():
                self.Refresh()
                info = [i.text(), self.win, self.dsamp]
                functions.BJ_octave(i, self.table, self.h, self.dsamp, self.num, self.step, info)

    def Margenau_Hill_octave(self):
        for i in self.arr:
            if i.isChecked():
                self.Refresh()
                info = [i.text(), self.win, self.dsamp]
                functions.MH_octave(i, self.table, self.h, self.dsamp, self.num, self.step, info)

    def Pseudo_Wigner_Ville_octave(self):

        for i in self.arr:
            if i.isChecked():
                self.Refresh()
                info = [i.text(), self.win, self.dsamp]
                functions.PWV_octave(i, self.table, self.h, self.dsamp, self.num, self.step, info)

    def Pseudo_Page_octave(self):

        for i in self.arr:
            if i.isChecked():
                self.Refresh()
                info = [i.text(), self.win, self.dsamp]
                functions.PPage_octave(i, self.table, self.h, self.dsamp, self.num, self.step, info)

    def Choi_Williams_octave(self):

        for i in self.arr:
            if i.isChecked():
                self.Refresh()
                info = [i.text(), self.win, self.dsamp]
                functions.CW_octave(i, self.table, self.h, self.dsamp, self.num, self.step, info)

    def Margenau_Hill_Spectrogram_octave(self):

        for i in self.arr:
            if i.isChecked():
                self.Refresh()
                info = [i.text(), self.win, self.dsamp]
                functions.MHS_octave(i, self.table, self.h, self.dsamp, self.num, self.step, info)

    def Page_octave(self):

        for i in self.arr:
            if i.isChecked():
                self.Refresh()
                info = [i.text(), self.win, self.dsamp]
                functions.Page_octave(i, self.table, self.h, self.dsamp, self.num, self.step, info)

    def Spectrogram_octave(self):

        for i in self.arr:
            if i.isChecked():
                self.Refresh()
                info = [i.text(), self.win, self.dsamp]
                functions.Spectr_octave(i, self.table, self.h, self.dsamp, self.num, self.step, info)

    def Pseudo_Margenau_Hill_octave(self):

        for i in self.arr:
            if i.isChecked():
                self.Refresh()
                info = [i.text(), self.win, self.dsamp]
                functions.PMH_octave(i, self.table, self.h, self.dsamp, self.num, self.step, info)

    def Zhao_Atlas_Marks_octave(self):

        for i in self.arr:
            if i.isChecked():
                self.Refresh()
                info = [i.text(), self.win, self.dsamp]
                functions.ZAM_octave(i, self.table, self.h, self.dsamp, self.num, self.step, info)

    def Rihaczek_octave(self):

        for i in self.arr:
            if i.isChecked():
                self.Refresh()
                info = [i.text(), self.win, self.dsamp]
                functions.Ri_octave(i, self.table, self.h, self.dsamp, self.num, self.step, info)

    """New functions Plot"""

    def limitation_area(self):
        arr = [self.lineEdit_y_start, self.lineEdit_y_stop, \
               self.lineEdit_x_start, self.lineEdit_x_stop]
        limit_arr = []

        for i in arr:

            if i.text().isdecimal() or "-" in i.text():
                limit_arr.append(float(i.text()))
            else:
                limit_arr.append(None)

        return limit_arr

    def Histogram_function(self):
        list_for_func = []
        for i in self.arr:
            if i.isChecked():
                list_for_func.append(i.text())

        functions.hist(list_for_func,int(self.lineEdit_bins.text()), self.limitation_area())

    def Entropy_function(self):
        list_for_func = []
        for i in self.arr:
            if i.isChecked():
                list_for_func.append(i.text())
        functions.entropy(list_for_func,int(self.lineEdit_bins.text()), self.limitation_area())

    def Escort_function(self):
        list_for_func = []
        for i in self.arr:
            if i.isChecked():
                list_for_func.append(i.text())
        functions.escort(list_for_func)

    def Inf_Dicrimination_function(self):
        list_for_func = []
        for i in self.arr:
            if i.isChecked():
                list_for_func.append(i.text())
        functions.information_discrimination(list_for_func)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    Form = Functionality()
    # MainWindow = QtWidgets.QMainWindow()
    # ui = Functionality()
    Form.show()
    # MainWindow.show()
    sys.exit(app.exec_())
